import 'package:flutter/material.dart';
import 'base_layout.dart';

enum GridLayoutType { byCount, byExtend }

/// 表格部局
class GridLayout extends BaseLayout {
  //部局类型，byCount每交叉轴显示个数，byExtend 根据每个子组件最大宽度
  final GridLayoutType gridLayoutType;
  //滚动方向
  final Axis scrollDirection;
  //是否反转显示
  final bool reverse;
  //grid 控制器
  final ScrollController controller;
  //最大子组件宽度，只用于byExtend
  final double maxCrossAxisExtent;
  //每交叉轴显示子组件个数，只用于byCount
  final int crossAxisCount;
  //主轴子组件间距
  final double mainAxisSpacing;
  //交叉轴子组件间距
  final double crossAxisSpacing;
  //子组件宽高比
  final double childAspectRatio;
  //子组件集合，与builder属性只用一个
  final List<Widget> children;
  //子组件创建器，与children属性只用一个
  final IndexedWidgetBuilder builder;
  //子组件个数，只用于builder
  final int itemCount;

  GridLayout(
      {Key key,
      AlignmentGeometry alignment,
      double width,
      double height,
      bool isVisible,
      Decoration decoration,
      EdgeInsetsGeometry padding,
      EdgeInsetsGeometry margin,
      this.gridLayoutType = GridLayoutType.byCount,
      this.maxCrossAxisExtent,
      this.crossAxisCount = 3,
      this.children = const [],
      this.mainAxisSpacing = 0.0,
      this.crossAxisSpacing = 0.0,
      this.childAspectRatio = 1.0,
      this.scrollDirection = Axis.vertical,
      this.controller,
      this.reverse = false,
      this.builder,
      this.itemCount})
      : assert((builder != null && itemCount != null) || children.length > 0),
        super(
            key: key,
            alignment: alignment,
            width: width,
            height: height,
            isVisible: isVisible,
            padding: padding,
            margin: margin,
            decoration: decoration);
  @override
  _GridLayoutState createState() => _GridLayoutState();
}

class _GridLayoutState extends State<GridLayout> with BaseState {
  @override
  void initState() {
    baseLayout = widget;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return buildState(buildContent);
  }

  @override
  Widget buildContent() {
    return widget.gridLayoutType == GridLayoutType.byExtend
        ? _buildSliverGridDelegateWithMaxCrossAxisExtent
        : _buildSliverGridDelegateWithFixedCrossAxisCount();
  }

  _buildSliverGridDelegateWithFixedCrossAxisCount() {
    return widget.builder == null
        ? GridView.count(
            crossAxisCount: widget.crossAxisCount,
            children: widget.children,
            mainAxisSpacing: widget.mainAxisSpacing,
            crossAxisSpacing: widget.crossAxisSpacing,
            childAspectRatio: widget.childAspectRatio,
            scrollDirection: widget.scrollDirection,
            controller: widget.controller,
            reverse: widget.reverse,
          )
        : GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: widget.crossAxisCount,
              mainAxisSpacing: widget.mainAxisSpacing,
              crossAxisSpacing: widget.crossAxisSpacing,
              childAspectRatio: widget.childAspectRatio,
            ),
            itemBuilder: widget.builder,
            itemCount: widget.itemCount,
            controller: widget.controller,
            reverse: widget.reverse,
            scrollDirection: widget.scrollDirection,
          );
  }

  _buildSliverGridDelegateWithMaxCrossAxisExtent() {
    return widget.builder == null
        ? GridView.extent(
            maxCrossAxisExtent: widget.maxCrossAxisExtent,
            children: widget.children,
            mainAxisSpacing: widget.mainAxisSpacing,
            crossAxisSpacing: widget.crossAxisSpacing,
            childAspectRatio: widget.childAspectRatio,
            scrollDirection: widget.scrollDirection,
            controller: widget.controller,
            reverse: widget.reverse,
          )
        : GridView.builder(
            gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
              maxCrossAxisExtent: widget.maxCrossAxisExtent,
              mainAxisSpacing: widget.mainAxisSpacing,
              crossAxisSpacing: widget.crossAxisSpacing,
              childAspectRatio: widget.childAspectRatio,
            ),
            itemBuilder: widget.builder,
            itemCount: widget.itemCount,
            controller: widget.controller,
            reverse: widget.reverse,
            scrollDirection: widget.scrollDirection,
          );
  }
}
